package com.dau.system.controller;

import cn.hutool.core.lang.UUID;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
@ApiOperation(value = "测试系统是否运行")
public class TestController {
    @Resource
    RabbitTemplate rabbitTemplate;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("string")
    public String string() {
        return "hello word";
    }

    @GetMapping("redis")
    public String redis() {
        redisTemplate.opsForValue().set("test", "hello word");
        return redisTemplate.opsForValue().get("test");
    }

    @GetMapping("/rabbit/send")
    public void test() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：NewDirectRouting 发送到交换机NewDirectExchange
        rabbitTemplate.convertAndSend("NewDirectExchange", "NewDirectRouting", map);
    }
}
